package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.viewpager.widget.ViewPager;

import com.dtsz.m2.R;
import com.dtsz.m2.ui.adapter.GameAdatper;
import com.dtsz.m2.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class GameRoleActivity extends BaseActivity {
    private ViewPager vpContent;
    private GameAdatper gameAdatper;
    private List<View> mViewList;

    private LinearLayout inLl;
    private int mCurrentItem =0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_ganme_role);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("游戏角色");
        mViewList = new ArrayList<View>();
        LayoutInflater lf = getLayoutInflater().from(this);
        View view1 = lf.inflate(R.layout.item_game_role, null);
        View view2 = lf.inflate(R.layout.item_game_role1, null);
        View view3 = lf.inflate(R.layout.item_game_role2, null);
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        gameAdatper = new GameAdatper(mViewList);
        vpContent.setAdapter(gameAdatper);
        setIndicator();

    }

    @Override
    public void initView() {
        super.initView();
        vpContent = findViewById(R.id.vp_content);
        inLl = findViewById(R.id.in_ll);
        vpContent.setPageMargin(20);//设置page间间距，自行根据需求设置
        vpContent.setOffscreenPageLimit(3);//>=3
    }

    /**
     * 动态添加指示器
     */
    private void setIndicator() {
        View viewIndicator;
        for (int i = 0; i < mViewList.size(); i++) {
            //创建imageview作为小圆点
            viewIndicator = new View(this);
            //设置默认背景
            viewIndicator.setBackgroundResource(R.drawable.selector_ling_hui);
            viewIndicator.setEnabled(false);
            //设置指示器宽高
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 10);
            //除了第一个小圆点，其他小圆点都设置边距
            if (i != 0) {
                layoutParams.leftMargin = 32;
            }
            //设置布局参数
            viewIndicator.setLayoutParams(layoutParams);
            //添加指示器到布局
            inLl.addView(viewIndicator);
        }

        //默认选中第一个指示器
        inLl.getChildAt(0).setEnabled(true);

        //监听滑动
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                inLl.getChildAt(mCurrentItem).setEnabled(false);
                inLl.getChildAt(i).setEnabled(true);
                mCurrentItem = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }



}

